home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MACD 5
/
MACD 5.bin
/
workbench
/
libs
/
usergrouplib.lha
/
usergroup
/
time.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-09-02
|
6KB
|
206 lines
RCS_ID_C="$Id: time.c,v 2.1 1994/02/17 02:21:58 ppessi Exp $";
/*
* time.c --- GMT time functions
*
* Author: ppessi <Pekka.Pessi@hut.fi>
*
* This file is part of the AmiTCP/IP User Library.
*
* Copyright © 1993 AmiTCP/IP Group, <AmiTCP-Group@hut.fi>
* Helsinki University of Technology, Finland.
*
* Created : Wed Sep 15 01:25:26 1993 ppessi
* Last modified: Thu Jan 27 12:12:57 1994 ppessi
*/
#include <sys/errno.h>
#include <assert.h>
#include <sys/time.h>
#include <devices/timer.h>
#include "base.h"
#include "libfunc.h"
/*
* Time zone support for the gettimeofday. Zeroes default to the GMT
* without daylight saving.
*/
static struct timezone __time_zone = {0,0};
/*
* Seconds to to the system time (seconds from 00:00 1.1.1978)
* to the GMT (seconds from 00:00 1.1.1970).
* _STIopenTimer() adds the local time seconds west from GMT to this
* value, so the local time gets converted to the GMT.
*/
#define AMIGA_OFFSET ((8L*365 + 8/4)*24*60*60)
static long __local_to_GMT = AMIGA_OFFSET;
static struct timerequest timereq[1] = { 0 };
struct Library *TimerBase;
/* This structure must only be allocated by locale.library and is READ-ONLY! */
struct Locale
{
STRPTR loc_LocaleName; /* locale's name */
STRPTR loc_LanguageName; /* language of this locale */
STRPTR loc_PrefLanguages[10]; /* preferred languages */
ULONG loc_Flags; /* always 0 for now */
ULONG loc_CodeSet; /* always 0 for now */
ULONG loc_CountryCode; /* user's country code */
ULONG loc_TelephoneCode; /* country's telephone code */
LONG loc_GMTOffset; /* minutes from GMT */
/* deleted the rest to save space */
};
void CloseLocale( struct Locale *locale );
struct Locale *OpenLocale( STRPTR name );
#pragma libcall LocaleBase CloseLocale 2A 801
#pragma libcall LocaleBase OpenLocale 9C 801
int TimeInit(void)
{
if (OpenDevice(TIMERNAME, UNIT_VBLANK, (struct IORequest *)timereq, 0)) {
TimerBase = NULL;
return -1;
} else {
TimerBase = (struct Library*)timereq->tr_node.io_Device;
if (TimerBase->lib_Version >= 36) {
/*
* Initialize time zone information for the gettimeofday()
* First try to open locale (2.1 and up), and if that fails,
* try to read environment variable TZ.
*/
void *LocaleBase;
struct Locale *thisLocale = NULL;
if ((LocaleBase = OpenLibrary("locale.library", 38)) != NULL) {
if ((thisLocale = OpenLocale(NULL)) != NULL) {
/*
* Update time zone minutes west from GMT.
*/
__time_zone.tz_minuteswest = thisLocale->loc_GMTOffset;
CloseLocale(thisLocale);
}
CloseLibrary(LocaleBase);
}
if (!thisLocale) { /* if locale information was not available */
short len;
long value;
char zone[10];
BPTR file = Open("ENV:TZ", MODE_OLDFILE);
if (file) {
len = Read(file, zone, sizeof(zone));
if (len > 3) {
zone[len] = '\000';
/* should interpret floats as well! */
if (StrToLong(zone+3, &value) > 0) {
/*
* Update time zone minutes west from GMT.
*/
__time_zone.tz_minuteswest = (short)value * (short)60;
}
}
Close(file);
}
}
/*
* Update local time seconds to GMT translation
*/
__local_to_GMT += (short)__time_zone.tz_minuteswest * (short)60;
return 0;
}
return -1;
}
}
void TimeCleanup(void)
{
if (TimerBase) {
CloseDevice((struct IORequest *)timereq), TimerBase = NULL;
}
}
/****i* usergroup.library/gettimeofday *************************************
NAME
gettimeofday - get date and time
SYNOPSIS
#include <sys/time.h>
success = gettimeofday(tvp, tzp)
D0 A0 A1
int gettimeofday(struct timeval *, struct timezone *)
DESCRIPTION
The system's notion of the current Greenwich time and the current
time zone is obtained with the gettimeofday() call. The time is
expressed in seconds and microseconds since midnight (0 hour),
January 1, 1970. The resolution of the system clock is hardware
dependent, and the time may be updated continuously or in `ticks.'
If tp or tzp is NULL, the associated time information will not be
returned or set.
The structures pointed to by tp and tzp are defined in <sys/time.h>
as:
struct timeval {
long tv_sec; \* seconds since Jan. 1, 1970 *\
long tv_usec; \* and microseconds *\
};
struct timezone {
int tz_minuteswest; \* of Greenwich *\
int tz_dsttime; \* type of dst correction to apply *\
};
The timezone structure indicates the local time zone (measured in
minutes of time westward from Greenwich), and a flag that, if
nonzero, indicates that Daylight Saving time applies locally during
the appropriate part of the year.
RETURN
A 0 return value indicates that the call succeeded. A -1 return
value indicates an error occurred, and in this case an error code is
available by ug_GetErr() call and it may be stored into the global
variable errno.
ERRORS
The following error codes may be available by ug_GetErr() or stored
in errno:
[EFAULT] An argument address referenced invalid memory.
****************************************************************************
*/
SAVEDS ASM int R_gettimeofday(REG(a0) struct timeval *tvp,
REG(a1) struct timezone *tzp)
{
if (tvp) {
GetSysTime(tvp);
tvp->tv_sec += __local_to_GMT;
}
if (tzp)
*tzp = __time_zone;
return 0;
}
SAVEDS ASM int R_settimeofday(REG(a0) struct timeval *tvp,
REG(a1) struct timezone *tzp)
{
return -1;
}